<template>
  <!--商品详情-->
  <div class="product-detail">
    <!--公用头部组件-->
    <custom-header :title="'商品详情'"></custom-header>
    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <!--轮播图组件-->
        <van-swipe class="my-swipe" indicator-color="#1baeae">
          <van-swipe-item v-for="(item, index) in detail.goodsCarouselList" :key="index">
            <img :src="item" alt=""/>
          </van-swipe-item>
        </van-swipe>
      </div>
      <!--商品信息描述-->
      <div class="product-info">
        <div class="product-title">
          {{ detail.goodsName || '' }}
        </div>
        <div class="product-desc">免邮费，顺丰快递</div>
        <div class="product-price">
          <span>￥{{ detail.sellingPrice || ''}}</span>
        </div>
      </div>
      <!--商品参数-->
      <div class="product-intro">
        <ul>
          <li>概述</li>
          <li>参数</li>
          <li>安装服务</li>
          <li>常见问题</li>
        </ul>
        <!--详情页面-->
        <div class="product-content" v-html="detail.goodsDetailContent || ''"></div>
      </div>
    </div>
    <!--底部操作按钮，需要提前在main.js里注册好vant ui组件-->
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服"/>
      <!--count参数，设置购物车图标右上角的商品数量-->
      <van-action-bar-icon icon="cart-o" :badge="!count ? '' : count" text="购物车" @click="goTo"/>
      <van-action-bar-button type="warning" text="加入购物车" @click="handleAddCart"/>
      <van-action-bar-button type="danger" text="立即购买" @click="handleBuy"/>
    </van-action-bar>
  </div>
</template>
<script>
import { getDetail } from 'service/good' //详情接口方法
import { reactive, onMounted, toRefs, getCurrentInstance, computed } from 'vue'
import CustomHeader from 'components/CustomHeader.vue' //公用头部
import { addCart } from '@/service/cart'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { useRouter } from 'vue-router' //路由插件
import { Toast } from 'vant'


export default {
  name: 'ProductDetail',
  components: {
    CustomHeader //注册组件
  },
  setup(){
    //通过vue提供的getCurrentInstance方法，在setup函数内获取main.js入口脚本中定义的全局方法
    const { appContext: { config: { globalProperties }}} = getCurrentInstance();
    const route = useRoute() //生成路由实例
    const state = reactive({
      detail: {
        goodsCarouselList: [] //声明录播图数组
      }
    })
    const store = useStore() //生成store实例
    const router = useRouter() //生成路由实例
    //初始化方法
    onMounted(() => {
      initData()
    })
    //获取详情数据
    const initData = async () => {
      const { id } = route.params 
      const { data } = await getDetail(id)
      /**
       * data数据结构
       * {
       *  goodsCarouselList: 轮播图数组
       *  goodsCoverImg: 商品的默认展示图片
       *  goodsDetailContent: 商品详情介绍
       *  goodsId: 商品ID
       *  goodsIntro: 商品简介
       *  goodsName: 商品名称
       *  originalPrice: 商品原价
       *  sellingPrice: 商品单价
       *  tag: 商品标签
       * }
       */
      //过滤出图片
      data.goodsCarouselList = data.goodsCarouselList.map(i => globalProperties.$filters.prefix(i))
      state.detail = data
      //更新购物车商品数量
      store.dispatch('updateCart')
    }
    //前往购物车页面
    const goTo = () => {
      router.push({path: '/cart'})
    }
    //添加购物车方法
    const handleAddCart = async () => {
      const { resultCode } = await addCart({ goodsCount: 1, goodsId: state.detail.goodsId })
      if(resultCode == 200) {
        Toast.success('添加成功')
      }
      //添加完毕后，更新购物车商品数量
      store.dispatch('updateCart')
    }
    //立即购买
    const handleBuy = async () => {
      await addCart ({ goodsCount: 1, goodsId: state.detail.goodsId})
      //调用立即购买结构，更新购物中的商品数量
      store.dispatch('updateCart')
      router.push({path: '/cart'})
    }
    //通过计算方法computed 监听 store.state.cartCount购物车商品数量的变化
    const count = computed(() => {
      console.log('cartCount', store.state.cartCount)
      return store.state.cartCount
    })

    return {
      ...toRefs(state),
      goTo,
      handleAddCart,
      handleBuy,
      count
    }
  }
}
</script>
<style lang="less">
@import '../theme/custom';
.product-detail{
  .detail-header{
    .fj();
    .wh(100%, 44px);
    line-height: 44px;
    padding: 0 10px;
    .boxSizing();
    color: #252525;
    background: #fff;
    border-bottom: 1px solid #dcdcdc;
    .product-name{
      font-size: 14px;
    }
  }
  .detail-content{
    height: calc(100vh - 50px);
    overflow: hidden;
    overflow-y: auto;
    .detail-swipe-wrap{
      .my-swipe .van-swipe-item {
        img{
          width: 100%;
          //height: 300px;
        }
      }
    }
    .product-info{
      padding: 0 10px;
      .product-title{
        font-size: 18px;
        text-align: left;
        color: #333;
      }
      .product-desc{
        font-size: 14px;
        text-align: left;
        color: #999;
        padding: 5px 0;
      }
      .product-price{
        .fj();
        span:nth-child(1){
          color: #F63515;
          font-size: 22px;
        }
        span:nth-child(2){
          color: #999;
          font-size: 16px;
        }
      }
    }
    .product-intro{
      width: 100%;
      padding-bottom: 50px;
      ul{
        .fj();
        width: 100%;
        margin: 10px 0;
        li{
          flex: 1;
          padding: 5px 0;
          text-align: center;
          font-size: 15px;
          border-right: 1px solid #999;
          box-sizing: border-box;
          &:last-child{
            border-right: none;
          }
        }
      }
      .product-content{
        padding: 0 20px;
        img{
          width: 100%;
        }
      }
    }
  }
  .van-action-bar-button--warning{
    background: linear-gradient(to right, #6bd8d8, @primary)
  }
  .van-action-bar-button--danger{
    background: linear-gradient(to right, #0dc3c3, #098888)
  }
}
</style>